// -*- indent-tabs-mode: nil -*-
using System;
using System.Collections;
using System.Collections.Specialized;
using NUnit.Framework;
using BZ = seanfoy.oopsnet.Bugzilla;

namespace seanfoy.oopsnet {
    [TestFixture]
    public class TestBugzilla {
        [Test]
        public void BasicCNF() {
            IDictionary result = new ListDictionary();
            ArrayList conj = new ArrayList();
            BZ.addCNF(result, conj);
            Assert.AreEqual(0, result.Count, "addCNF should tolerate trivial instances");
            ArrayList dis = new ArrayList();
            BZ.Triple t = new BZ.Triple("a", "b", "c");
            dis.Add(t);
            conj.Add(dis);
            BZ.addCNF(result, conj);
            Assert.AreEqual(3, result.Count, "addCNF should tolerate non-trivial instances");
        }

        [Test]
        public void SloppyCNF() {
            IDictionary result = new ListDictionary();
            ArrayList conj = new ArrayList();
            BZ.Triple t = new BZ.Triple("a", "b", "c");
            conj.Add(t);
            BZ.addCNF(result, conj);
            Assert.AreEqual(3, result.Count, "addCNF should understand Triples nested directly within the conjunction as singleton disjunctions.");
        }

        [Test]
        public void BZKeyNames() {
            IDictionary result = new ListDictionary();
            ArrayList conj =
                BZ.listify(
                    BZ.listify(
                        new BZ.Triple("f0", "o0", "v0"),
                        new BZ.Triple("f1", "o1", "v1")),
                    new BZ.Triple("f2", "o2", "v2"),
                    new BZ.Triple("f3", "o3", "v3"));
            BZ.addCNF(result, conj);
            Assert.AreEqual(4 * 3, result.Count);
            for (int i = 0; i < conj.Count; ++i) {
                foreach (string part in new string [] {"field", "type", "value"}) {
                    Assert.IsNotNull(
                        result.Contains(
                            String.Format(
                                "{0}0-{1}-0",
                                part,
                                i)));
                }
            }
        }

        [Test]
        public void oneSpecificQuery() {
            IDictionary result = new ListDictionary();
            ArrayList conj =
                BZ.listify(
                    new BZ.Triple("short_desc", "casesubstring", "SomeException"),
                    BZ.listify(
                        new BZ.Triple("bug_file_loc", "substring", "/some/url"),
                        new BZ.Triple("short_desc", "anywords", "important words")));
            BZ.addCNF(result, conj);
            // BZ and .NET use differing case for url escape codes
            StringAssert.AreEqualIgnoringCase(
                "field0-0-0=short_desc&type0-0-0=casesubstring&value0-0-0=SomeException&field0-1-0=bug_file_loc&type0-1-0=substring&value0-1-0=%2Fsome%2Furl&field0-1-1=short_desc&type0-1-1=anywords&value0-1-1=important+words",
                BZ.querystring(result));
        }
    }
}
